<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="数据库唯一标志" label-width="120px" prop="dbid">
        <el-input
          v-model="queryParams.dbid"
          placeholder="请输入数据库唯一标志"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用途" prop="useFor">
        <el-input
          v-model="queryParams.useFor"
          placeholder="请输入数据库用途"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dbconfigList" height="330px">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据库唯一标志" align="center" prop="dbid" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="初始连接数" align="center" prop="initSize" />
      <el-table-column label="最大连接数" align="center" prop="maxActive" />
      <el-table-column label="最小连接数" align="center" prop="minIdle" />
      <el-table-column label="数据库驱动" align="center" prop="driverClassName" />
      <el-table-column label="用途" align="center" prop="useFor" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dbconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleTest(scope.row)"
            v-hasPermi="['system:dbconfig:list']"
          >测试</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:log:list']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改数据库配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="唯一标志" prop="dbid">{{ form.dbid }}</el-form-item>
        <el-form-item label="连接信息" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd" type="password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="qrpwd">
          <el-input v-model="form.qrpwd" type="password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="初始连接数" prop="initSize">
          <el-input v-model="form.initSize" placeholder="请输入初始连接数" />
        </el-form-item>
        <el-form-item label="最大连接数" prop="maxActive">
          <el-input v-model="form.maxActive" placeholder="请输入最大连接数" />
        </el-form-item>
        <el-form-item label="最小连接数" prop="minIdle">
          <el-input v-model="form.minIdle" placeholder="请输入最小连接数" />
        </el-form-item>
        <el-form-item label="数据库驱动" prop="driverClassName">
          <el-input v-model="form.driverClassName" placeholder="请输入数据库驱动" />
        </el-form-item>
        <el-form-item label="验证sql" prop="validationQuery">
          <el-input v-model="form.validationQuery" placeholder="请输入验证sql" />
        </el-form-item>
        <el-form-item label="用途" prop="useFor">
          <el-input v-model="form.useFor" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查询配置明细对话框 -->
    <el-dialog :title="title" :visible.sync="detailopen" width="700px" append-to-body>
      <el-form ref="detailform" :model="form" label-width="100px" size="mini">
        <el-col :span="24">
          <el-form-item label="唯一标志：">{{ form.dbid }} </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="连接信息：">{{ form.url }} </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名：">{{ form.username }} </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始连接数：">{{ form.initSize }} </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大连接数：">{{ form.maxActive }} </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最小连接数：">{{ form.minIdle }} </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="数据库驱动：">{{ form.driverClassName }} </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="验证sql：">{{ form.validationQuery }} </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用途：">{{ form.useFor }} </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailopen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDbconfig, getDbconfig, testDbconfig, updateDbconfig } from "@/api/system/dbconfig";

export default {
  name: "Dbconfig",
  data() {
    let confirmPwd = (rule, value, callback) => {
        if(value === undefined || value === null || value === "") {
          callback(new Error("请再次输入密码！"));
        } else if(value !== this.form.pwd) {
          callback(new Error("两次输入密码不一致，请重新输入！"));
        } else {
          callback();
        }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据库配置表格数据
      dbconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看明细弹出层
      detailopen: false,
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        dbid: null,
        useFor: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        url: [
          { required: true, message: "数据库连接不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        qrpwd: [
          { required: true, validator: confirmPwd, trigger: "blur" }
        ],
        initSize: [
          { required: true, message: "初始连接数不能为空", trigger: "blur" }
        ],
        maxActive: [
          { required: true, message: "最大连接数不能为空", trigger: "blur" }
        ],
        minIdle: [
          { required: true, message: "最小连接数不能为空", trigger: "blur" }
        ],
        driverClassName: [
          { required: true, message: "数据库驱动不能为空", trigger: "blur" }
        ],
        validationQuery: [
          { required: true, message: "验证sql不能为空", trigger: "blur" }
        ],
        useFor: [
          { required: true, message: "用途不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据库配置列表 */
    getList() {
      this.loading = true;
      listDbconfig(this.pageParams,this.queryParams).then(response => {
        this.dbconfigList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dbid: null,
        url: null,
        username: null,
        pwd: null,
        qrpwd: null,
        initSize: null,
        maxActive: null,
        minIdle: null,
        driverClassName: null,
        validationQuery: null,
        useFor: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      var dbidUpdate = row.dbid;
      if(row.isTrusted){
        dbidUpdate = this.ids[0];
      }
      this.queryParams["dbid"] = dbidUpdate;
      getDbconfig(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据库配置";
        this.queryParams["dbid"] = null;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateDbconfig(this.form).then(response => {
            if(response.code==='200'&&response.data>0) {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }else{
              this.$modal.msgError("修改失败");
              this.open = false;
            }
          });
        }
      });
    },
    /** 测试按钮操作 */
    handleTest(row) {
      var dbids = this.ids;
      if(row.dbid){
        dbids = new Array();
        dbids.push(row.dbid);
      }
      testDbconfig(dbids).then(response => {
        if(response.code==='200') {
          this.$modal.msgSuccess(row.dbid + "测试通过");
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    },
    /** 查看详情操作 */
    handleView(row) {
      this.title = "查看详情";
      this.detailopen = true;
      this.form = row;
    }
  }
};
</script>
